package com.fw.util;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import org.apache.log4j.Logger;

public class StringUtils {

    private static Logger log = Logger.getLogger(StringUtils.class.getName());

    public static final String PATTERN_DATETIME_PERSISTENCE = "yyyyMMdd HH:mm:ss";

    public static final String PATTERN_DATE_PERSISTENCE = "yyyyMMdd";

    public static final String PATTERN_DATETIME_UI = "dd/MM/yyyy HH:mm:ss";

    public static final String PATTERN_DATE_UI = "dd/MM/yyyy";

    public static final String PATTERN_MONTH_YEAR_PERSISTENCE = "yyyyMM";

    public static final String PATTERN_MONTH_YEAR_UI = "MM/yyyy";

    public static final String PATTERN_DATETIME_INTERFACE = "yyyyMMddHHmmss";

    public static final String formatDate(Date date, String pattern) {
	SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern, Locale.ENGLISH);
	return simpleDateFormat.format(date);
    }

    public static final Date parseDate(String dateString, String pattern) {
	Date date = null;
	if (dateString != null && pattern != null) {
	    SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern, Locale.ENGLISH);
	    try {
		date = simpleDateFormat.parse(dateString);
	    } catch (ParseException e) {
		log.error(e.getMessage(), e);
	    }
	}
	return date;
    }

    public static String getCashFormat(String serviceId, String mcInv) {
	int stringSize = 25;
	String result = serviceId + mcInv;
	if (result.length() < stringSize) {
	    result = serviceId + org.apache.commons.lang.StringUtils.leftPad(mcInv, stringSize - result.length() + mcInv.length(), "0");
	} else {
	    result = result.substring(0, stringSize);
	}
	return result;
    }

    public static BigDecimal stringToBigDecimal(String val) {
	BigDecimal bigDecimal = null;
	if (val != null && val.length() > 0) {
	    val = val.replaceAll(",", "");
	    bigDecimal = new BigDecimal(val);
	}
	return bigDecimal;
    }
}
